package com.yantong.controller.api;

import com.yantong.pojo.Activity;
import com.yantong.service.ActivityService;
import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.service.LocalLifeService;
import com.yantong.pojo.TicketUsableTeam;
import com.yantong.pojo.TicketParam;
import com.yantong.service.TicketService;
import com.yantong.pojo.Member;
import com.yantong.service.MemberService;
import com.yantong.annotation.Auth;
import com.yantong.annotation.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Api(tags = "本地生活相关接口")
@Controller
@RequestMapping("/wxapi/localLife")
public class ApiLocalLifeController extends BaseController {

    @Autowired
    private LocalLifeService localLifeService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private TicketService ticketService;
    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port = 6379;

    //timeout for jedis try to connect to redis server, not expire time! In milliseconds
    @Value("${spring.redis.timeout}")
    private int timeout = 0;

    @Value("${spring.redis.password}")
    private String password;



    /**
     * 获取店铺信息
     *
     * @param shopId
     * @return
     */
    @ApiOperation(value = "本地生活--获取店铺信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺ID/teamId", defaultValue = "", paramType = "query", required = false)
    })
    @ResponseBody
    @GetMapping("/getStoreInfo")
    public ResultVo getStoreInfo(Long shopId) {
        return ResultVo.ok().put("data", memberService.getByKey(shopId));
    }

    /**
     * 获取大礼包列表
     *
     * @param shopId
     * @return
     */
    @ApiOperation(value = "本地生活--获取大礼包列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺ID/teamId", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "longitude", value = "店铺经度", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "latitude", value = "店铺纬度", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "hotFlag", value = "是否热门（0否 1是）", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "centreId", value = "商家id", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "offset", value = "分页开始索引", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "limit", value = "每页限制数量", defaultValue = "", paramType = "query", required = false)
    })
    @ResponseBody
    @GetMapping("/getGiftPackages")
    public ResultVo getGiftPackages(Integer plateType,Long shopId, String longitude,String latitude,Integer hotFlag,Long centreId, int offset, int limit,String sidx,String order) {
        return ResultVo.ok().put("data", localLifeService.getGiftPackages(plateType,shopId, longitude, latitude, hotFlag,centreId,offset, limit,sidx,order));
    }

    @ApiOperation(value = "本地生活--根据商品id获取大礼包列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "storeFreechargeId", value = "大礼包商品id", defaultValue = "", paramType = "query", required = false)
    })
    @ResponseBody
    @GetMapping("/getGiftPackagesByFreechargeId")
    public ResultVo getGiftPackagesByFreechargeId(Long storeFreechargeId) {
        return ResultVo.ok().put("data", localLifeService.getGiftPackagesByFreechargeId(storeFreechargeId));
    }

    @ApiOperation(value = "大礼包详情详情")
    @ResponseBody
    @GetMapping("/detail")
    @Auth
    public ResultVo detail(Long id) {
        Activity activity = new Activity();
        if (id == null) {
            return ResultVo.error("大礼包id不能为空");
        } else {
            activity = activityService.getActivityDetailById(id);
        }
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", activity);
        return resultVo;
    }

    @ApiOperation(value = "大礼包通兑券用户下单选择门店")
    @ResponseBody
    @GetMapping("/usable")
    //@Auth
    public ResultVo usable(Long id) {
        List<TicketUsableTeam> ticketUsableTeam = null;
        if (id == null) {
            return ResultVo.error("大礼包id不能为空");
        } else {
            ticketUsableTeam = activityService.getTicketUsableTeam(id);
        }
        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", ticketUsableTeam);
        return resultVo;
    }

    @ApiOperation(value = "到过的店铺")
    @ResponseBody
    @GetMapping("/getBuyShops")
    @Auth
    public ResultVo getBuyShops(Long mid) {
        if (mid == null) {
            return ResultVo.error("用户id不能为空");
        }

        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", activityService.getBuyShops(mid));
        return resultVo;
    }

    @ApiOperation(value = "根据优惠券的ID获取所有免单优惠券的列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ticketRecordId", value = "用户优惠券ID", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "offset", value = "分页开始索引", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "limit", value = "每页限制数量", defaultValue = "", paramType = "query", required = false)
    })
    @ResponseBody
    @GetMapping("/getLocalLifeCoupon")
    @Auth
    public ResultVo getLocalLifeCoupon(@ApiIgnore @LoginUser Member member, Long ticketRecordId, int offset, int limit) {
        if (ticketRecordId == null) {
            return ResultVo.error("优惠券id不能为空");
        }

        ResultVo resultVo = new ResultVo();
        resultVo.put("success", true);
        resultVo.put("data", ticketService.getLocalLifeCoupon(member.getId(), ticketRecordId, offset, limit));
        return resultVo;
    }

    /**
     * 获取店铺信息
     *
     * @param shopId
     * @return
     */
    @ApiOperation(value = "本地生活--获取店铺详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "店铺ID/teamId", defaultValue = "", paramType = "query", required = false)
    })
    @ResponseBody
    @GetMapping("/getStoreDetail")
    public ResultVo getStoreDetail(Long shopId) {
        return ResultVo.ok().put("data", memberService.getLocalStoreDetail(shopId));
    }

    /**
     * 获取店铺信息
     *
     * @return
     */
    @ApiOperation(value = "本地生活--获取用户的优惠券列表，以店铺为单位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "店铺优惠券所属用户ID", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "status", value = "店铺优惠券状态-1：已失效(已过期) 0未使用 1已使用 2未领取", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "offset", value = "分页开始索引", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "limit", value = "每页限制数量", defaultValue = "", paramType = "query", required = false)
    })
    @ResponseBody
    @GetMapping("/getShopCouponList")
    public ResultVo getShopCouponList(Long memberId,Integer status, int offset, int limit) {
        return ResultVo.ok().put("data", ticketService.getLocalLifShopCoupon(memberId,status,offset,limit));
    }

    /**
     * 获取店铺信息
     *
     * @return
     */
    @ApiOperation(value = "本地生活--获取订单对应的优惠券列表，以店铺为单位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单ID", defaultValue = "", paramType = "query", required = false),
            @ApiImplicitParam(name = "offset", value = "分页开始索引", defaultValue = "0", paramType = "query", required = true),
            @ApiImplicitParam(name = "limit", value = "每页限制数量", defaultValue = "10", paramType = "query", required = true)
    })
    @ResponseBody
    @GetMapping("/getOrderCouponList")
    public ResultVo getOrderCouponList(Long orderId,int offset, int limit) {
        return ResultVo.ok().put("data", ticketService.getOrderCouponList(orderId,offset,limit));
    }

    /**
     * 优惠券核销
     *
     * @param ticketParam
     * @return
     */
    @ApiOperation(value = "优惠券核销")
    @PostMapping("/writeOff")
    @ResponseBody
    @Auth
    public ResultVo writeOff(@LoginUser Member member,@RequestBody TicketParam ticketParam) {
        ticketService.writeOffTicketList(member,ticketParam);
        return ResultVo.ok();
    }

    /**
     * 获取用户分享海报
     *
     * @return
     */
    @ApiOperation(value = "获取用户分享海报")
    @PostMapping("/getPoster")
    @ResponseBody
    @Auth
    public ResultVo getPoster(@LoginUser Member member) {
        return ResultVo.ok().put("data","https://user.yantongtech.cn/bdsh/static/img/store1.880e2ca.png");
    }

}
